const mongoose = require('mongoose');
const User = require('./src/models/user.model');
const Shop = require('./src/models/shop.model');
const Product = require('./src/models/product.model');
require('dotenv').config();

async function createTestData() {
  try {
    console.log('连接数据库...');
    await mongoose.connect(process.env.MONGODB_URI);
    console.log('数据库连接成功');
    
    // 1. 创建管理员用户
    let admin = await User.findOne({ name: 'admin' });
    if (!admin) {
      admin = await User.create({
        name: 'admin',
        email: 'admin@trove.com',
        password: 'admin123',
        role: 'admin',
        isActive: true,
        isVerified: true
      });
      console.log('管理员用户创建成功');
    } else {
      console.log('管理员用户已存在');
    }
    
    // 2. 创建测试用户
    let testUser = await User.findOne({ email: 'test@shop.com' });
    if (!testUser) {
      testUser = await User.create({
        name: '测试店主',
        email: 'test@shop.com',
        password: 'password123',
        role: 'seller',
        isActive: true,
        isVerified: true
      });
      console.log('测试用户创建成功');
    } else {
      console.log('测试用户已存在');
    }
    
    // 3. 创建测试店铺
    let testShop = await Shop.findOne({ name: 'Apple官方旗舰店' });
    if (!testShop) {
      testShop = await Shop.create({
        name: 'Apple官方旗舰店',
        description: 'Apple官方授权旗舰店，正品保证',
        logo: 'https://via.placeholder.com/200x200/007AFF/fff?text=Apple',
        banner: 'https://via.placeholder.com/800x300/007AFF/fff?text=Apple+Store',
        owner: testUser._id,
        type: 'official',
        status: 'active',
        categories: ['手机数码'],
        rating: {
          overall: 4.9,
          service: 4.9,
          logistics: 4.8,
          description: 4.9
        }
      });
      console.log('测试店铺创建成功');
    } else {
      console.log('测试店铺已存在');
    }
    
    // 4. 创建测试商品
    const existingProducts = await Product.countDocuments();
    if (existingProducts === 0) {
      const testProducts = [
        {
          name: 'iPhone 15 Pro',
          description: '全新iPhone 15 Pro，搭载A17 Pro芯片',
          shop: testShop._id,
          price: 7999,
          originalPrice: 8999,
          images: ['https://via.placeholder.com/400x400/007AFF/fff?text=iPhone+15+Pro'],
          brand: 'Apple',
          category: '手机数码',
          countInStock: 100,
          isFeatured: true,
          tags: ['新品', '热销'],
          specifications: {
            '屏幕尺寸': '6.1英寸',
            '存储容量': '128GB',
            '颜色': '深空黑色'
          }
        },
        {
          name: 'MacBook Pro 14',
          description: 'MacBook Pro 14英寸，M3芯片',
          shop: testShop._id,
          price: 14999,
          originalPrice: 15999,
          images: ['https://via.placeholder.com/400x400/007AFF/fff?text=MacBook+Pro'],
          brand: 'Apple',
          category: '电脑办公',
          countInStock: 50,
          isFeatured: true,
          tags: ['专业', '高性能'],
          specifications: {
            '屏幕尺寸': '14英寸',
            '处理器': 'M3芯片',
            '内存': '16GB'
          }
        },
        {
          name: 'AirPods Pro',
          description: 'AirPods Pro 第二代，主动降噪',
          shop: testShop._id,
          price: 1899,
          originalPrice: 1999,
          images: ['https://via.placeholder.com/400x400/007AFF/fff?text=AirPods+Pro'],
          brand: 'Apple',
          category: '数码配件',
          countInStock: 200,
          isFeatured: false,
          tags: ['降噪', '无线'],
          specifications: {
            '连接方式': '蓝牙',
            '降噪': '主动降噪',
            '续航': '6小时'
          }
        }
      ];
      
      await Product.insertMany(testProducts);
      console.log('测试商品创建成功，共创建', testProducts.length, '个商品');
    } else {
      console.log('商品数据已存在，共', existingProducts, '个商品');
    }
    
    console.log('\n测试数据创建完成！');
    console.log('管理员登录信息:');
    console.log('用户名: admin');
    console.log('密码: admin123');
    
    process.exit(0);
  } catch (error) {
    console.error('创建测试数据失败:', error);
    process.exit(1);
  }
}

createTestData();
